package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.template.AliOssTemplate;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {
    @Autowired
    AliOssTemplate aliOssTemplate;
    @PostMapping("/upload")
    @ApiOperation("上传")
    public Result<String> upload(MultipartFile file) {
        try {
            String filename = file.getOriginalFilename();
            String substring = filename.substring(filename.indexOf("."));
            filename = UUID.randomUUID().toString()+substring;
            String oss = aliOssTemplate.oss(file.getInputStream(), filename);
            return Result.success(oss);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }


}
